//=============================================================================
//     _   _                               ____                _  _          
//    | | | |  __ _  _ __   _ __   _   _  / ___|  _ __   _ __ (_)| |_   ___ 
//    | |_| | / _` || '_ \ | '_ \ | | | | \___ \ | '_ \ | '__|| || __| / _ \
//    |  _  || (_| || |_) || |_) || |_| |  ___) || |_) || |   | || |_ |  __/
//    |_| |_| \__,_|| .__/ | .__/  \__, | |____/ | .__/ |_|   |_| \__| \___|
//                  |_|    |_|     |___/         |_|                         
//
//                     HappySprite - We make sprites happy
//
// Copyright (c) 2007 by Tank Monkey Games
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace HappySprite
{
    internal static class MemberHelper
    {
        const BindingFlags bindingFlags =
          BindingFlags.FlattenHierarchy |
          BindingFlags.Instance |
          BindingFlags.Public |
          BindingFlags.NonPublic;

        const MemberTypes memberTypes =
            MemberTypes.Field |
            MemberTypes.Property |
            MemberTypes.Method;

        internal static MemberInfo GetMember(object obj, string memberName)
        {
            Type type = obj.GetType();
            MemberInfo[] memberInfoArray = type.GetMember(memberName, memberTypes, bindingFlags);

            if (memberInfoArray.Length == 0)
                return null;

            return memberInfoArray[0];
        }

        internal static object InvokeMember(object obj, MemberInfo memberInfo, params object[] parameters)
        {
            object returnValue = null;

            switch (memberInfo.MemberType)
            {
                case MemberTypes.Field:
                    ((FieldInfo)memberInfo).SetValue(obj, parameters[0]);
                    break;

                case MemberTypes.Property:
                    ((PropertyInfo)memberInfo).SetValue(obj, parameters[0], null);
                    break;

                case MemberTypes.Method:
                    returnValue = ((MethodInfo)memberInfo).Invoke(obj, parameters);
                    break;
            }

            return returnValue;
        }
    }
}
